<?php
    
    namespace App\Http\Controllers\Cart;
    
    use App\Models\Address;
    use App\Models\Attribute;
    use App\Models\Cart;
    use App\Models\Collect;
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    
    class CartController extends Controller
    {
        //加入购物车
        public function addToCart($good_id, $product_id, $number, $attribute)
        {
//     需要商品id,货品id,选购商品数量,商品的属性组合,以及用户id
//     处理商品的属性组合
            
            $attributeArr = explode(',', $attribute);
//   根据货品id去判断购物车中,是否已存在该货品下的商品,存在就增加数量,不存在就添加这个货品组合
            $carts = Cart::where('product_id', $product_id)->first();
            if (!$carts) {
                Cart::create([
                    'user_id' => auth()->user()->id,
                    'goods_id' => $good_id,
                    'product_id' => $product_id,
                    'number' => $number,
                    'attributeArr' => $attributeArr,
                ]);
            } else {
                $number = intval($number);
//        dd($number+$carts['number']);
                $carts->update(['number' => 8]);
            }
//    dd($carts->toArray());
            $carts = Cart::where('user_id', auth()->user()->id)->with('getGoods')->with('getProducts')->get();
//    dd($carts);
            return view('cart.cart.list', compact('carts'));
        }
        
        public function list()
        {
            $carts = Cart::where('user_id', auth()->user()->id)->with('getGoods', 'getProducts', 'checkShoucang')->get()->toArray();
            
            foreach ($carts as $k => $cart) {
                $carts[$k]['attribute'] = Attribute::whereIn('id', $cart['get_products']['attributeArr'])->pluck('name')->toArray();
            }
            $carts = json_encode($carts);
            return view('cart.cart.list', compact('carts'));
        }
        
        //删除购物车里的一条数据
        public function removeCart($id)
        {
            $cart = Cart::where('id', $id)->where('user_id', auth()->user()->id)->first();
            $cart->delete();
            return ['date' => $cart->delete(), 'code' => 1];
        }
        
        //购物车商品数量加一
        public function changNum($type, Cart $cart)
        {
            $product = $cart->getProducts;
            if ($type == 'plus') {
                if ($cart['number'] >= $product['inventory']) {
                    return ['valid' => 0, 'message' => '库存不足'];
                }
                $cart->update(['number' => $cart['number'] + 1]);
            } else {
                if ($cart['number'] == 1) {
                    return ['valid' => 0, 'message' => '不能减少'];
                }
                $cart->update(['number' => $cart['number'] - 1]);
            }
            return ['valid' => 1, 'message' => '操作成功'];
        }
        
        public function deleteCart(Cart $cart)
        {
            $cart->delete();
            return ['valid' => 1, 'message' => '删除成功'];
        }
        
        //购物车选择的商品跳到结算页面
        public function settleAccounts($id, $total)
        {
            //已经获得购物车的商品id,和商品总价
            $ids = explode(',', $id);
//        dd($ids);
            //获取该用户所有的地址
            $address = Address::where('user_id', auth()->id())->get();
            $cart = Cart::whereIn('id', $ids)->with('getGoods', 'getProducts')->get()->toArray();
//        dump($cart);
            return view('order.order.confirmOrder', compact('cart', 'total', 'address', 'id'));
        }
    }
